package com.zhiqian.community.util;

public class RedisKeyUtil {
    private static final String SPLIT = ":";
    private static final String PREFIX_ENTITY_LIKE = "like:entity";
    private static final String PREFIX_USER_LIKE = "like:user";
    private static final String PREFIX_FOLLOWEE = "followee";
    private static final String PREFIX_FOLLOWER = "follower";
    private static final String PREFIX_KAPTCHA = "kaptcha";
    private static final String PREFIX_TICKET = "ticket";
    private static final String PREFIX_USER = "user";
    private static final String PREFIX_UV = "uv";
    private static final String PREFIX_DAU = "dau";
    private static final String PREFIX_VARIATION = "variation";
    private static final String PREFIX_FILE_DOWNLOAD_COUNT = "file";

    /**
     * 生成某个实体获赞集合的key
     * like:entity:entityType:entityId ->set(userId)
     *
     * @return
     */
    public static String getEntityLikeKey(int entityType, Integer entityId) {
        return PREFIX_ENTITY_LIKE + SPLIT + entityType + SPLIT + entityId;
    }


    /**
     * 生成某个用户获赞数量的key
     * like:user:userId ->int
     *
     * @param userId
     * @return
     */
    public static String getUserHaveLikeCountKey(Integer userId) {
        return PREFIX_USER_LIKE + SPLIT + userId;
    }

    /**
     * 生成某个用户关注的实体key
     * followee:userId:entityType ->zSet(entityId,date)
     *
     * @param userId
     * @param entityType
     * @return
     */
    public static String getFolloweeKey(Integer userId, int entityType) {
        return PREFIX_FOLLOWEE + SPLIT + userId + SPLIT + entityType;
    }

    /**
     * 生成某个实体的粉丝key
     * follower:entityType:entityId ->zSet(userId,date)
     *
     * @param entityType
     * @param entityId
     * @return
     */
    public static String getFollowerKey(int entityType, Integer entityId) {
        return PREFIX_FOLLOWER + SPLIT + entityType + SPLIT + entityId;
    }

    /**
     * 生成登陆验证码的key
     * kaptcha:owner  ->string
     *
     * @param owner
     * @return
     */
    public static String getKaptchaKey(String owner) {
        return PREFIX_KAPTCHA + SPLIT + owner;
    }

    /**
     * 生成用户登录ticket的key
     * ticket:ticket  ->string(user)
     *
     * @param ticket
     * @return
     */
    public static String getTicketUserKey(String ticket) {
        return PREFIX_TICKET + SPLIT + ticket;
    }

    /**
     * 生成用户缓存的key
     * user:userId  ->string(user)
     *
     * @param userId
     * @return
     */
    public static String getUserKey(Integer userId){
        return PREFIX_USER + SPLIT + userId;
    }

    /**
     * 获取当天存放 UV 数据的key
     * @return
     */
    public static String getUVKey(String date){
        return PREFIX_UV + SPLIT + date;
    }

    /**
     * 获取某个时间段的 UV key
     * @param startDate
     * @param endDate
     * @return
     */
    public static String getUVKey(String startDate,String endDate){
        if (startDate.equals(endDate)){
            return PREFIX_UV + SPLIT + startDate;
        }
        return  PREFIX_UV + SPLIT + startDate + SPLIT + endDate;
    }

    /**
     * 获取当天存放 DAU 数据的key
     * @return
     */
    public static String getDAUKey(String date){
        return PREFIX_DAU + SPLIT + date;
    }

    /**
     * 获取某个时间段的 DAU key
     * @param startDate
     * @param endDate
     * @return
     */
    public static String getDAUKey(String startDate,String endDate){
        if (startDate.equals(endDate)){
            return PREFIX_DAU + SPLIT + startDate;
        }
        return  PREFIX_DAU + SPLIT + startDate + SPLIT + endDate;
    }

    /**
     * 获取分数有变动的帖子集合的key
     * @return
     */
    public static String getVariationPostKey(){
        return PREFIX_VARIATION + SPLIT +"post";
    }

    /**
     * 获取某个文件的下载次数的key
     * @param filePath
     * @return
     */
    public static String getFileDownloadCountKey(String filePath){
        return PREFIX_FILE_DOWNLOAD_COUNT + SPLIT +filePath;
    }
}
